package customermanagesystem.demo.controller;

import customermanagesystem.demo.bean.User2;
import customermanagesystem.demo.dao.User2AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    User2AdminMapper user2AdminMapper;

    @GetMapping("/login")
    public String getlogin(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(Integer username, String password, HttpSession session)
    {
//        List<User2> list= user2AdminMapper.sel();
        List<User2> list=user2AdminMapper.sel();
        ModelAndView mav = new ModelAndView();
        for(int i=0;i<list.size();i++)
        {
            User2 user=new User2();
            user=list.get(i);
            if(user.getPhone().equals(username))
            {
                if(user.getUserpassword().equals(password))
                {
                    session.setAttribute("user",username);
                    if(user.getUserlimit()==0)
                    {
                        mav.setViewName("index");
                        return mav;
                    }
                    if(user.getUserlimit()==1)
                    {
                        mav.setViewName("productManager");
                    }
                }
                else if(!user.getUserpassword().equals(password))
                {
                    mav.addObject("hl","用户名错误");
                }
            }else if(list.size()==i+1 &&!user.getPhone().equals(username)){
                mav.addObject("hl","密码错误");
            }
        }
        return mav;
    }
//    @RequestMapping("/login")
//    @ResponseBody
//    public ResponseEntity login(User2 user, HttpServletResponse response, HttpSession session){
////        System.out.println(user);
////        System.out.println(userPassword);
//        if(user.getPhone()!=null&&user.getUserpassword()!=""){
//            List<User2> users = user2AdminMapper.findUserByPhone(user.getPhone());
//            if(users!=null){
//                for(User2 user2 : users){
//                    if (user2.getUserpassword().equals(user.getUserpassword())) {
////                        Cookie cookie = new Cookie("phone",user.getPhone());
////                        response.addCookie(cookie);
//                        session.setAttribute("user",user.getPhone());
////                        System.out.println("已创建");
//                        return new ResponseEntity(HttpStatus.OK);
//                    }
//                }
//            }
//
//            return new ResponseEntity("用户名或密码不正确", HttpStatus.INTERNAL_SERVER_ERROR);
////            System.out.println("用户名或密码不正确");
////            return null;
//        }
//        return new ResponseEntity("用户名或密码不能为空", HttpStatus.INTERNAL_SERVER_ERROR);
////        System.out.println("用户名或密码不能为空");
////        return null;
//    }


//    @RequestMapping("regist")
//    @ResponseBody
//    public String regist(Integer username,String password,String repassword)
//    {
//        User2 user=new User2();
//        user.setUserpassword(password);
//        user.setPhone(username);
//        if(repassword.equals(user.getUserpassword()))
//        {
//            user2AdminMapper.up(user);
//        }
//        if(!repassword.equals(user.getUserpassword()))
//        {
//            return "请重新确认密码";
//        }
//        return "注册成功";
//    }


    @PostMapping("/logout")
    @ResponseBody
    public void logout(HttpServletResponse response,HttpServletRequest request){
//        Cookie cookie = new Cookie("userCode","");
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);
        request.getSession().invalidate();//将session设置失效
    }







    @RequestMapping("/verify")

    @ResponseBody
    public String varify(String name)
    {

        System.out.println("电话"+name);
        List<User2> list= user2AdminMapper.sel();
        for(int i=0;i<list.size();i++)
        {
            User2 user=list.get(i);
            if(user.getPhone().toString().equals(name))
            {
                return "用户名正确";
            }
            if(!user.getPhone().toString().equals(name)&&list.size()==i+1)
            {
                return "用户名错误";
            }
        }
        System.out.println(name);
        return "manager";
    }
    @RequestMapping("/pverify")
    @ResponseBody
    public String pverify(String name,String psw){
        List<User2> list= user2AdminMapper.sel();
        for(int i=0;i<list.size();i++)
        {
            User2 user=list.get(i);
            if(user.getPhone().toString().equals(name))
            {
                if (user.getUserpassword().equals(psw))
                {
                    System.out.println("密码正确");
                    return "密码正确";
                }
                if (!user.getUserpassword().equals(psw))
                {
                    System.out.println("密码错误");
                    return "密码错误请重试！";
                }
            }
            if(!user.getPhone().toString().equals(name)&&list.size()==i+1){
                return "用户名错误请重试";
            }

        }
        return "错误";
    }
    @RequestMapping("/reLogin")
    public String reLogin()
    {
        return "login";
    }
    @RequestMapping("/usregist")
    @ResponseBody
    public String resLogin(String name)
    {
        if (name.equals("")){
            return "用户名不能为空";
        }
        System.out.println(name);
        List<User2> list= user2AdminMapper.sel();
        for(int i=0;i<list.size();i++)
        {
            User2 user = list.get(i);
            if (user.getPhone().toString().equals(name))
            {
                return "用户名已存在请重新输入";
            }

        }

        return "用户名可用";
    }

    @RequestMapping("/regist")
    @ResponseBody
    public String regist(String username,String password,String repassword)
    {
        List<User2> list= user2AdminMapper.sel();
        System.out.println(username+password+repassword);
        if(!password.equals(repassword))
        {
            return "请重新确认密码";
        }
        if(username.equals(""))
        {
            return "用户名不能为空";
        }
        for(int i=0;i<list.size();i++)
        {
            User2 user=list.get(i);
            if (user.getPhone().toString().equals(username)) {
                return "用户名已存在请重新输入";
            }
        }
//        User2 user=new User2();
        User2 user =new User2();
        user.setUserpassword(password);
        user.setPhone(Integer.valueOf(username));
        user2AdminMapper.up(user);


        return "注册成功";










    }
}
